package com.fanhao.dllo.justforyou.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.fanhao.dllo.justforyou.BuildConfig;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.entity.NewsViewPageInforPhotos;
import com.fanhao.dllo.justforyou.entity.NewsViewPageInforResult;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/4.
 */
public class NewsViewPageInforAdapter extends RecyclerView.Adapter<
        NewsViewPageInforAdapter.NewsViewPageInforViewHolder> {

    private List<NewsViewPageInforPhotos> photos;
    private Context context;
    private ImageLoader imageLoader;


    // 初始化imageLoader
    public NewsViewPageInforAdapter(Context context){
        this.context = context;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }


    // 更新数据
    public void addData(List<NewsViewPageInforPhotos> datas) {
        photos = datas;
        notifyDataSetChanged();
    }


    /***
     * 组件缓存内部类
     * ***/
    public class NewsViewPageInforViewHolder extends RecyclerView.ViewHolder {
        private TextView noteTv;
        private NetworkImageView imgIv;
        private int position;

        public NewsViewPageInforViewHolder(View v) {
            super(v);

            noteTv = (TextView)v.findViewById(R.id.noteTv);
            imgIv = (NetworkImageView) v.findViewById(R.id.imgIv);

        }
    }


    /****
     * 绑定布局
     * ***/
    @Override
    public NewsViewPageInforAdapter.NewsViewPageInforViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.activity_news_viewpage__item_infor,null);

        return new NewsViewPageInforViewHolder(v);
    }

    @Override
    public void onBindViewHolder(NewsViewPageInforAdapter.NewsViewPageInforViewHolder holder, int position) {
        if (photos != null) {
            NewsViewPageInforPhotos newsViewPageInforPhotos = photos.get(position);
            holder.noteTv.setText(newsViewPageInforPhotos.getNote());

            if (newsViewPageInforPhotos.getImgurl() != null &&
                    newsViewPageInforPhotos.getImgurl().length() > 0) {
                String imgUrl = newsViewPageInforPhotos.getImgurl();
                holder.imgIv.setImageUrl(imgUrl,imageLoader);

            }
           holder.position = position;

        }
    }

    @Override
    public int getItemCount() {
        return photos != null &&
                photos.size() > 0
                ? photos.size() : 0;
    }
}

